/*
 *      Copyright (c) 2018-2028, Chill Zhuang All rights reserved.
 *
 *  Redistribution and use in source and binary forms, with or without
 *  modification, are permitted provided that the following conditions are met:
 *
 *  Redistributions of source code must retain the above copyright notice,
 *  this list of conditions and the following disclaimer.
 *  Redistributions in binary form must reproduce the above copyright
 *  notice, this list of conditions and the following disclaimer in the
 *  documentation and/or other materials provided with the distribution.
 *  Neither the name of the dreamlu.net developer nor the names of its
 *  contributors may be used to endorse or promote products derived from
 *  this software without specific prior written permission.
 *  Author: Chill 庄骞 (smallchill@163.com)
 */
package com.plian.system.controller.zp;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.plian.Tools.Func;
import com.plian.Tools.StringPool;
import com.plian.Tools.base.BaseController;
import com.plian.Tools.pagedata.PageData;
import com.plian.system.annotation.LogOperation;
import com.plian.system.config.Resubmit;
import com.plian.system.entity.zp.ZpPlan;
import com.plian.system.entity.zp.ZpPlanSituation;
import com.plian.system.model.CommonCode;
import com.plian.system.model.ListResult;
import com.plian.system.model.R;
import com.plian.system.mybatis.search.SearchEntity;
import com.plian.system.mybatis.search.SearchParamEntiy;
import com.plian.system.mybatis.search.TokenSearch;
import com.plian.system.mybatis.support.Condition;
import com.plian.system.mybatis.support.Query;
import com.plian.system.service.zp.IZpPlanService;
import com.plian.system.service.zp.IZpPlanSituationService;
import com.plian.system.vo.zp.ZpPlanSituationVO;
import com.plian.system.vo.zp.ZpPlanVO;
import com.plian.system.wrapper.zp.ZpPlanSituationWrapper;
import com.plian.system.wrapper.zp.ZpPlanWrapper;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.AllArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.List;

/**
 * 招聘情况 控制器
 */
@RestController
@AllArgsConstructor
@RequestMapping("api/zpPlanSituation")
@Api(value = "招聘情况", tags = "招聘情况接口")
public class ZpPlanSituationController extends BaseController {

    private final IZpPlanSituationService zpPlanSituationService;

    private final ZpPlanSituationWrapper zpPlanSituationWrapper;

    private final IZpPlanService zpPlanService;

    /**
     * 详情
     */
    @PostMapping("/detail")
    @ApiOperation(value = "详情", notes = "传入annualWorkPlan")
    public R<ZpPlanSituationVO> detail(@RequestBody ZpPlanSituation annualWorkPlan) {
        ZpPlanSituation detail = zpPlanSituationService.getOne(Condition.getQueryWrapper(annualWorkPlan));
        ZpPlan byId = zpPlanService.getById(detail.getHirePlanId());
        ZpPlanSituationVO zpPlanSituationVO = zpPlanSituationWrapper.entityToVO(detail);
        zpPlanSituationVO.setHirePlanName(byId.getHirePlanName());
        zpPlanSituationVO.setHirePlanCount(byId.getHireNumber());
        return R.data(zpPlanSituationVO);
    }

    /**
     * 分页 招聘情况
     */
    @PostMapping("/list")
    @ApiOperation(value = "分页", notes = "传入annualWorkPlan")
    public ListResult<ZpPlanSituationVO> list(@RequestBody Query query) {
        List<SearchEntity> entityList = query.getSearch();
        if (query.getAscs() == null && query.getDescs() == null) {
            query.setDescs("create_time");
        }
        QueryWrapper<ZpPlanSituation> queryWrapper = SearchParamEntiy.getQueryWrapper(
                Condition.getQueryWrapper(new ZpPlanSituation()), entityList
        );
        TokenSearch<ZpPlanSituation> tokenSearch = new TokenSearch<>();
        tokenSearch.addTokenQuery(queryWrapper, query);

        IPage<ZpPlanSituation> pages = zpPlanSituationService.page(Condition.getPage(query), queryWrapper);
        ListResult<ZpPlanSituationVO> listResult = new ListResult<>(CommonCode.SUCCESS);
        List<ZpPlanSituationVO> zpPlanSituationVOS = zpPlanSituationWrapper.entityToVO(pages.getRecords());
        for (ZpPlanSituationVO zpPlanSituationVO : zpPlanSituationVOS) {
            ZpPlan byId = zpPlanService.getById(zpPlanSituationVO.getHirePlanId());
            zpPlanSituationVO.setHirePlanName(byId.getHirePlanName());
            zpPlanSituationVO.setHirePlanCount(byId.getHireNumber());
        }
        listResult.setList(zpPlanSituationVOS);
        listResult.setTotal(Long.valueOf(pages.getTotal()).intValue());

        return listResult;
    }

    /**
     * 新增或修改 招聘情况
     */
    @PostMapping("/submit")
    @Resubmit(delaySeconds = 0)
    @ApiOperation(value = "新增或修改", notes = "传入annualWorkPlan")
    @LogOperation("招聘情况-提交")
    public R submit(@Valid @RequestBody ZpPlanSituation annualWorkPlan) {
        return R.status(zpPlanSituationService.submit(annualWorkPlan));
    }


    /**
     * 删除 招聘情况
     */
    @PostMapping("/remove")
    @ApiOperation(value = "逻辑删除", notes = "传入ids")
    @LogOperation("招聘情况-删除")
    public R remove(@RequestBody PageData pd) {
        List<String> ids = (List<String>) pd.get("ids");
        return R.status(zpPlanSituationService.deleteLogic(Func.toLongList(StringUtils.join(ids, StringPool.COMMA))));
    }


}
